package com.jltx.xq.simplewatch.main;

import android.os.Bundle;
import android.view.View;

import com.jltx.xq.simplewatch.R;
import com.jltx.xq.simplewatch.base.BaseSupportFragment;
import com.jltx.xq.simplewatch.base.presenter.BasePresenter;
import com.jltx.xq.simplewatch.entity.event.MainActivityDrawerSwitchEvent;
import com.jltx.xq.simplewatch.home.HomeFragment;
import com.jltx.xq.simplewatch.live.LiveFragment;
import com.jltx.xq.simplewatch.main.adapter.MainFragmentPagerAdapter;
import com.jltx.xq.simplewatch.user.UserFragment;
import com.jltx.xq.simplewatch.video.ui.VideoFragment;
import com.jltx.xq.simplewatch.widget.SwipeableViewPager;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.jltx.xq.bottombarlayout.BottomBarLayout;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2017/08/17.
 */

public class MainFragment extends BaseSupportFragment {
    @BindView(R.id.bottom_bar_frg_main)
    BottomBarLayout bottomBarLayout;
    @BindView(R.id.viewpager_frg_main)
    SwipeableViewPager viewPager;

    private List<MainFragmentPagerAdapter.MainTabFragmentInfo> fragmentInfoList;
    private MainFragmentPagerAdapter fragmentPagerAdapter;

    public static MainFragment newInstance() {
        MainFragment fragment = new MainFragment();
        return fragment;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_main_act;
    }

    @Override
    public BasePresenter initPresenter() {
        return null;
    }

    @Override
    public void initView(View rootView, Bundle savedInstanceState) {
        fragmentInfoList =  new ArrayList<>();
        if(savedInstanceState == null){

            MainFragmentPagerAdapter.MainTabFragmentInfo info = new MainFragmentPagerAdapter.MainTabFragmentInfo(HomeFragment.newInstance(),R.string.bottom_bar_home_frg_main,R.drawable.home_normal,R.drawable.home_press,R.color.colorFrgMainBottomItemNor,R.color.colorFrgMainBottomItemSel);
            fragmentInfoList.add(info);

            info = new MainFragmentPagerAdapter.MainTabFragmentInfo(VideoFragment.newInstance(),R.string.bottom_bar_video_frg_main,R.drawable.video_normal,R.drawable.video_press,R.color.colorFrgMainBottomItemNor,R.color.colorFrgMainBottomItemSel);
            fragmentInfoList.add(info);

            info = new MainFragmentPagerAdapter.MainTabFragmentInfo(LiveFragment.newInstance(),R.string.bottom_bar_live_frg_main,R.drawable.live_normal,R.drawable.live_press,R.color.colorFrgMainBottomItemNor,R.color.colorFrgMainBottomItemSel);
            fragmentInfoList.add(info);

            info = new MainFragmentPagerAdapter.MainTabFragmentInfo(UserFragment.newInstance(),R.string.bottom_bar_user_frg_main,R.drawable.user_normal,R.drawable.user_press,R.color.colorFrgMainBottomItemNor,R.color.colorFrgMainBottomItemSel);
            fragmentInfoList.add(info);

        }else {

            MainFragmentPagerAdapter.MainTabFragmentInfo info = new MainFragmentPagerAdapter.MainTabFragmentInfo(findFragment(HomeFragment.class),R.string.bottom_bar_home_frg_main,R.drawable.home_normal,R.drawable.home_press,R.color.colorFrgMainBottomItemNor,R.color.colorFrgMainBottomItemSel);
            fragmentInfoList.add(info);

            info = new MainFragmentPagerAdapter.MainTabFragmentInfo(findFragment(VideoFragment.class),R.string.bottom_bar_video_frg_main,R.drawable.video_normal,R.drawable.video_press,R.color.colorFrgMainBottomItemNor,R.color.colorFrgMainBottomItemSel);
            fragmentInfoList.add(info);

            info = new MainFragmentPagerAdapter.MainTabFragmentInfo(findFragment(LiveFragment.class),R.string.bottom_bar_live_frg_main,R.drawable.live_normal,R.drawable.live_press,R.color.colorFrgMainBottomItemNor,R.color.colorFrgMainBottomItemSel);
            fragmentInfoList.add(info);

            info = new MainFragmentPagerAdapter.MainTabFragmentInfo(findFragment(UserFragment.class),R.string.bottom_bar_user_frg_main,R.drawable.user_normal,R.drawable.user_press,R.color.colorFrgMainBottomItemNor,R.color.colorFrgMainBottomItemSel);
            fragmentInfoList.add(info);

        }

        fragmentPagerAdapter = new MainFragmentPagerAdapter(getChildFragmentManager(),getActivity(),fragmentInfoList);
        viewPager.setOffscreenPageLimit(fragmentInfoList.size());
        viewPager.setAdapter(fragmentPagerAdapter);
        bottomBarLayout.setViewPager(viewPager);
        bottomBarLayout.setOnTabSelectedListener(new BottomBarLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position, int prePosition) {
                postMainActivityDrawerSwitchEvent(position == 0);
                //当页签切换的时候，如果有播放视频，则释放资源
                JCVideoPlayer.releaseAllVideos();
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });
        postMainActivityDrawerSwitchEvent(bottomBarLayout.getCurrentPosition() == 0);
    }

    /**
     * 发送是否关闭侧滑功能事件
     * @param isEnable
     */
    private void postMainActivityDrawerSwitchEvent(boolean isEnable){
        MainActivityDrawerSwitchEvent event = new MainActivityDrawerSwitchEvent();
        event.setEnable(isEnable);
        EventBus.getDefault().postSticky(event);
    }

}
